#include "main.h"
#include <string.h>
#include <stdio.h>
#include "bsp_qspi_w25q256.h"
#include "quadspi.h"


/* 要写入到FLASH的字符串数组 */
const uint8_t g_text_buf[] = {"MiniPRO STM32H7 QSPI TEST"};

#define TEXT_SIZE sizeof(g_text_buf) /* TEXT字符串长度 */

uint8_t data[0x10];
uint8_t datatemp[TEXT_SIZE];

__attribute__((section(".qspi_text"))) int extflash_test(int a, int b)
{
    return a + b;
}

__attribute__((section(".qspi_data"))) const uint8_t g_text_buf1[] = {"MiniPRO STM32H7 QSPI TEST1"};

void test()
{
    uint32_t flashsize = 0;
    flashsize = 16 * 1024 * 1024; /* FLASH 大小为16M字节 */

    // BSP_QSPI_Init();
    volatile uint32_t id = QSPI_FLASH_ReadID();
    volatile uint32_t device_id = QSPI_FLASH_ReadDeviceID();

    // extern uint32_t QSPI_ReadID();
    // volatile uint32_t id1 = QSPI_ReadID(&hqspi);
    volatile uint32_t id = QSPI_ReadID();

    // extern void QSPI_MemoryMapped();
    // QSPI_MemoryMapped();

    BSP_QSPI_Read(datatemp, 0, sizeof(g_text_buf));
    BSP_QSPI_Write((uint8_t *)g_text_buf, 0, sizeof(g_text_buf));
    BSP_QSPI_Read(datatemp, 0, sizeof(g_text_buf));
    BSP_QSPI_Erase_Sector(0);
    BSP_QSPI_Read(datatemp, 0, sizeof(g_text_buf));
    BSP_QSPI_Write((uint8_t *)g_text_buf, 0, sizeof(g_text_buf));
    BSP_QSPI_Read(datatemp, 0, sizeof(g_text_buf));
    BSP_QSPI_Erase_Block(0);
    // BSP_QSPI_Erase_Chip();
    BSP_QSPI_Read(datatemp, 0, sizeof(g_text_buf));

    QSPI_WriteBuffer((uint8_t *)g_text_buf, 0, sizeof(g_text_buf));
    QSPI_ReadBuffer(datatemp, 0, sizeof(g_text_buf));
    QSPI_EraseSector(0);
    // QSPI_EraseChip();
    QSPI_ReadBuffer(datatemp, 0, sizeof(g_text_buf));


    volatile int x = extflash_test(1, 2);

    // QSPI_Memory_Mapped();
    __asm__("NOP");
    /********************************************************/

    // extern void W25QXX_Qspi_Enable(void);
    // W25QXX_Qspi_Enable(); // 使能QSPI模式

    // __HAL_RCC_QSPI_FORCE_RESET();
    // __HAL_RCC_QSPI_RELEASE_RESET();

    // extern void W25QXX_Qspi_Enable(void);
    // W25QXX_Qspi_Enable();

    // extern uint8_t BSP_QSPI_Init(void);
    // BSP_QSPI_Init();

    // extern uint16_t W25QXX_ReadID(void);
    // extern uint32_t QSPI_FLASH_ReadDeviceID(void);
    // volatile uint32_t id3 = QSPI_FLASH_ReadDeviceID();
    // volatile uint16_t id2 = norflash_read_id();
    // volatile uint16_t id = W25QXX_ReadID();
    // volatile uint16_t id1 = W25Q256_ReadID();
    // volatile uint16_t id0 = QSPI_ReadID(&hqspi);

    // extern char W25Q_EraseSingleBlock(uint32_t uBlock);
    // W25Q_EraseSingleBlock(0);

    // extern char W25Q_ReadData(uint32_t ReadAddr, uint32_t NumByteToRead, uint8_t *pBuffer);

    // memset(data, 0, sizeof(data));
    // W25Q_ReadData(0, sizeof(data), data);
    // extern uint16_t norflash_ex_read_id(void);
    // __IO uint16_t id = norflash_ex_read_id();

    // extern void norflash_ex_erase_chip(void);
    // norflash_ex_erase_chip();

    // sprintf((char *)datatemp, "%s%d", (char *)g_text_buf, 0);

    // norflash_ex_write((uint8_t *)datatemp, flashsize - 100, TEXT_SIZE);
    // norflash_ex_read(datatemp, flashsize - 100, TEXT_SIZE);

    // extern void bsp_InitQSPI_W25Q256(QSPI_HandleTypeDef * hqspi);
    // bsp_InitQSPI_W25Q256(&hqspi);

    // uint8_t res = 0;

    // __ASM volatile("cpsid i");               /* 关闭中断 */
    // SCB_InvalidateICache();                  /* 清空I CACHE */
    // SCB_InvalidateDCache();                  /* 清空D CACHE */
    // QUADSPI->CR &= ~(1 << 0);                /* 关闭 QSPI 接口 */
    // QUADSPI->CR |= 1 << 1;                   /* 退出MEMMAPED模式 */
    // res = qspi_wait_flag(1 << 5, 0, 0XFFFF); /* 等待BUSY空闲 */

    // if (res == 0)
    // {
    //     QUADSPI->CCR = 0;      /* CCR寄存器清零 */
    //     QUADSPI->CR |= 1 << 0; /* 使能 QSPI 接口 */
    // }

    // volatile uint32_t id0 = QSPI_ReadID(&hqspi);
    // QSPI_EraseChip(&hqspi);

    // /* Enable write operations ------------------------------------------- */
    // QSPI_WriteEnable(&hqspi);

    // /* Erasing Sequence -------------------------------------------------- */
    // sCommand.Instruction = SECTOR_ERASE_CMD;
    // sCommand.AddressMode = QSPI_ADDRESS_1_LINE;
    // sCommand.Address = address;
    // sCommand.DataMode = QSPI_DATA_NONE;
    // sCommand.DummyCycles = 0;

    // if (HAL_QSPI_Command_IT(&QSPIHandle, &sCommand) != HAL_OK)
    // {
    //   Error_Handler();
    // }
}